<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Test Page for geoPolygonUtils.geometry.Segment class</title>
  <script language="JavaScript" type="text/javascript"
      src="../tools/jsunit/app/jsUnitCore.js"></script>
  <script language="JavaScript" type="text/javascript"
      src="test-utils.js"></script>
  <script language="JavaScript" type="text/javascript"
      src="../src/geometry.js"></script>
  <script language="JavaScript" type="text/javascript">

    // Alias for the Segment class.
    var Segment = geoPolygonUtils.geometry.Segment;

    
    // Alias for the Vector class.
    var Vector = geoPolygonUtils.geometry.Vector;

    
    function testSegmentConstructor() {
      var segment = new Segment(new Vector(0, 1), new Vector(2, 3));
      assertVectorCoords(0, 1, segment.a);
      assertVectorCoords(2, 3, segment.b);
    };
    
    
    function testSegmentLine() {
      var segment = new Segment(new Vector(0, 1), new Vector(0, 1));
      assertNull('no line for a zero length segment', segment.line());
      
      segment = new Segment(new Vector(1, 1), new Vector(5, 4));
      assertVectorCoords(-0.6, 0.8, segment.line().normal);
      assertAlmostEquals(-0.2, segment.line().c);
    };
    
    
    function testSegmentCenter() {
      assertVectorCoords(666, 13, new Segment(
          new Vector(237, -77), new Vector(1095, 103)).center());
    };
    
    
    function testDistanceToPoint() {
      var segment = new Segment(new Vector(1, 1), new Vector(5, 4));
      assertAlmostEquals(0, segment.distanceToPoint(new Vector(3, 2.5)));
      assertAlmostEquals(5, segment.distanceToPoint(new Vector(0, 6.5)));
      assertAlmostEquals(5, segment.distanceToPoint(new Vector(-2, -3)));
      assertAlmostEquals(5, segment.distanceToPoint(new Vector(8, 8)));

      segment = new Segment(new Vector(0, 1), new Vector(0, 1));
      assertAlmostEquals(10, segment.distanceToPoint(new Vector(10, 1)));
    };
    
    
    function testContainsPoint() {
      var segment = new Segment(new Vector(1, 1), new Vector(5, 4));
      assertTrue(segment.containsPoint(segment.a));
      assertTrue(segment.containsPoint(segment.b));
      
      assertTrue(segment.containsPoint(new Vector(3, 2.5)));
      assertFalse(segment.containsPoint(new Vector(3, 2.51)));
      assertTrue(segment.containsPoint(new Vector(3, 2.51), 0.1));
      var precision = new geoPolygonUtils.geometry.Precision(1e-9, 0.1, 1e-9);
      assertTrue(segment.containsPoint(new Vector(3, 2.51), precision));
    };
    
    
    function testIsEndpoint() {
      var segment = new Segment(new Vector(1, 1), new Vector(5, 4));
      assertTrue(segment.isEndpoint(segment.a));
      assertTrue(segment.isEndpoint(segment.b));
      assertFalse(segment.isEndpoint(segment.center()));

      assertFalse(segment.containsPoint(new Vector(1, 1.01)));
      assertTrue(segment.containsPoint(new Vector(1, 1.01), 0.1));
      var precision = new geoPolygonUtils.geometry.Precision(1e-9, 0.1, 1e-9);
      assertTrue(segment.containsPoint(new Vector(1, 1.01), precision));
    };
    
    
    function testSegmentLength() {
      assertAlmostEquals(0, new Segment(
          new Vector(3.45, 21.71), new Vector(3.45, 21.71)).length());
      assertAlmostEquals(5, new Segment(
          new Vector(1, 1), new Vector(4, 5)).length());
    };
    
    
    function testIntersectSegment() {
      var point = new Segment(new Vector(1, 1), new Vector(5, 4))
          .intersectSegment(new Segment(new Vector(1, 4), new Vector(5, 1)));
      assertNotNull(point);
      assertVectorCoords(3, 2.5, point);
      
      point = new Segment(new Vector(1, 1), new Vector(5, 4))
          .intersectSegment(new Segment(new Vector(-4, -1), new Vector(0, -4)));
      assertNull('segments don\'t intersect', point);

      point = new Segment(new Vector(1, 1), new Vector(5, 4))
          .intersectSegment(new Segment(new Vector(2, 1), new Vector(6, 4)));
      assertNull('segments parallel', point);

      point = new Segment(new Vector(1, 1), new Vector(5, 4))
          .intersectSegment(new Segment(new Vector(1, 1), new Vector(-3, -2)));
      assertNull('segments overlap', point);
    };
    
  </script>
  <style>
    .code {
      color: green;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <h1>Test Page for <span class="code">geoPolygonUtils.geometry.Segment</span>
  class.</h1>
  <p>This page contains tests for the methods of <span class="code">
  geoPolygonUtils.geometry.Segment</span> class. To see them take a look at the
  source of the page.</p>
  <p><em>Note:</em> this file is intended to be used with
  <a href="http://www.jsunit.net/">JSUnit</a> test framework.</p>
</body>
</html>
